<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Click and Drag</title>
		<link
			rel="stylesheet"
			href="style.css" />
		<link
			rel="icon"
			href="https://fav.farm/🔥" />
	</head>
	<body>
		<div class="items">
			<div class="item item1">01</div>
			<div class="item item2">02</div>
			<div class="item item3">03</div>
			<div class="item item4">04</div>
			<div class="item item5">05</div>
			<div class="item item6">06</div>
			<div class="item item7">07</div>
			<div class="item item8">08</div>
			<div class="item item9">09</div>
			<div class="item item10">10</div>
			<div class="item item11">11</div>
			<div class="item item12">12</div>
			<div class="item item13">13</div>
			<div class="item item14">14</div>
			<div class="item item15">15</div>
			<div class="item item16">16</div>
			<div class="item item17">17</div>
			<div class="item item18">18</div>
			<div class="item item19">19</div>
			<div class="item item20">20</div>
			<div class="item item21">21</div>
			<div class="item item22">22</div>
			<div class="item item23">23</div>
			<div class="item item24">24</div>
			<div class="item item25">25</div>
		</div>

		<script type="module">
			let slideItems = document.querySelector('.items');
			let startX = 0;
			let scrollLeft = 0;
			let isStart = false;

			slideItems.addEventListener('mousedown', (e) => {
				isStart = true;
				startX = e.clientX - slideItems.offsetLeft;
				slideItems.classList.add('active');
				scrollLeft = slideItems.scrollLeft;
			});

			slideItems.addEventListener('mouseup', (e) => {
				isStart = false;
				slideItems.classList.remove('active');
			});
			slideItems.addEventListener('mouseleave', (e) => {
				isStart = false;
				slideItems.classList.remove('active');
			});

			slideItems.addEventListener('mousemove', (e) => {
				//一定要阻止事件冒泡
				if (!isStart) return;
				let x = e.clientX - slideItems.offsetLeft;
				let walk = -(x - startX) * 3;
				slideItems.scrollLeft = scrollLeft + walk;
			});
		</script>
	</body>
</html>
